package sheepmix;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

import connexionServer.Serveur;
import connexionServer.ServeurUDP;


import bdd.DBConnection;



import sheepmixView.client.model.Musique;
import treeInspector.TreeInspector;


import LecteurMp3.MultiPlayer;



public class Server {

	/**
	 * @param args
	 */
	
	static MultiPlayer m;
	static TreeInspector ti;
	static LinkedList<Musique> pathList, temp;
	public static void main(String[] args) {
		
		System.out.println("Lancement du serveur en cours...");
		System.out.println("Veuillez patienter svp.");
		
		final InputStreamReader isr=new InputStreamReader(System.in);
		final BufferedReader br=new BufferedReader(isr);
	    final String serveur  = "localhost";
		final String bdd      = "sheepmixdb";
		final int    port     = 3306;
		final String user     = "sheepmix_soft";
		final String password = "sheeppass1401";
		

		Thread t1 = new Thread() {
			
			public void run() {
				while(true) {
					try {
						String line = br.readLine();
						
						if(line.equalsIgnoreCase("d")) {
							//System.out.println(m.displayableList());
							m.nextOne();
						}
						
						if(line.equalsIgnoreCase("stop")) {
							m.stopPlaylist();
						}
						
						if(line.equalsIgnoreCase("play")) {
							m.startPlaylist();
						}
						
						if(line.equalsIgnoreCase("usr")) {
							
						}
						
						if(line.equalsIgnoreCase("c")) {
							Musique cu = m.getPlayedMusic();
							System.out.println(cu.getHTMLTitle());

						}
						
						if(line.equalsIgnoreCase("ls")) {
							LinkedList<Musique> l = m.getPlaylist(15);
							for(int i = 0; i < l.size(); i++) {
								System.out.println(l.get(i).getHTMLTitle());
							}
						}
						
						if(line.equalsIgnoreCase("replace")) {
							m.removeList();
							m.addPlaylist(temp);
						}
						
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}	
				
			}
		};

		t1.start();
		m = new MultiPlayer();
		try {
			System.out.print("Scanning file-system...                ");
			ti = new TreeInspector("C:\\Users\\Alex\\Desktop\\sheepzik"); // Repertoire musique
			int b = ti.mettreAJour();
			pathList = new LinkedList<Musique>();
			System.out.println("   --done");
			System.out.println("");
			
			System.out.print("Conecting to database...               ");
			DBConnection dbc = new DBConnection(serveur,bdd,port,user,password);
			dbc.connect();
			System.out.println("   --done");
			System.out.println("");
			
			System.out.println("Scan result : "+b);
			if(b == 1) {
				System.out.println("Tree has been changed !");
				System.out.print("Delete removed entries from database...");
				dbc.removeFiles(ti.getElementsSuprimmes());
				System.out.println("   --done");
				System.out.print("Add new items to database...           ");
				dbc.addFiles(ti.getNouveauxElements());
			}
			
			System.out.println("Starting UDP Server...");
			new ServeurUDP(6666);
			temp = new LinkedList<Musique>();
			temp = dbc.getArtistTracks("Little Boots");
			
			System.out.println("Starting TCP Server...");
		    new Serveur(dbc,m);
		    
		    System.out.println("");
		    System.out.println("");
		    System.out.println("=======================================");
		    System.out.println("=          SheepMix Server v1.0       =");
		    System.out.println("=                                     =");
		    System.out.println("= Authors :                           =");
		    System.out.println("=      --ARBELET Alexandre            =");
		    System.out.println("=      --MOEGLI  Thomas               =");
		    System.out.println("=      --VIBERT  Sandy                =");
		    System.out.println("=                                     =");
		    System.out.println("= Compagny :                          =");
		    System.out.println("=      --HEIG-VD                      =");
		    System.out.println("=                                     =");
		    System.out.println("= Professor :                         =");
		    System.out.println("=      --JATON Markus                 =");
		    System.out.println("=                                     =");
		    System.out.println("= Assistant :                         =");
		    System.out.println("=      --VINCENT Mark                 =");
		    System.out.println("=======================================");
		    System.out.println("");
		    System.out.println("");
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//for (int i = 0; i < ti.getArborescence().size(); i++) {
			//try {
				m.addPlaylist(pathList);
				//System.out.println("Nouvelle entrée : " + pathList.get(i));
			//}
			//catch(Exception e) {
				//System.out.println(e);
			//}
		//}
		
		m.startPlaylist();
		
		
	}

}
